﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;

namespace AutoZingMe
{
    public partial class frmMain : Form
    {

        [DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
        internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);

        [DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
        internal static extern IntPtr GetCurrentProcess();

        int pos = -1;
        int i = 0;
        ArrayList arrAcc;
        string[] acc;
        public frmMain()
        {
            InitializeComponent();
            dataGridView1.Rows.Add("Test", "test", Bitmap.FromFile("stop.png"));
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = true;
            btnStop.Enabled = false;
            i--;
            pos = 11;
            wbrAuto.Navigate("https://pay.zing.vn/home/logout.html");
        }

        public ArrayList ReadFile(string path)
        {
            ArrayList accounts = new ArrayList();
            string[] lines = System.IO.File.ReadAllLines(path);
            foreach (string line in lines)
            {
                accounts.Add(line);
            }
            return accounts;
        }

        private void wbrAuto_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            switch (pos)
            {
                case 0://Start
                    IntPtr pHandle = GetCurrentProcess();
                    SetProcessWorkingSetSize(pHandle, -1, -1);

                    pos = 1;
                    this.statusAutoText.Text = "START";
                    wbrAuto.Navigate("http://login.me.zing.vn");
                    break;
                case 1://Login
                    pos = 2;
                    this.statusAutoText.Text = "LOGIN";
                    try
                    {
                        acc = arrAcc[i].ToString().Split(' ');
                        wbrAuto.Document.GetElementById("u").SetAttribute("value", acc[0]);
                        wbrAuto.Document.GetElementById("pp").SetAttribute("value", acc[1]);
                        wbrAuto.Document.GetElementById("bntlogin").InvokeMember("Click");
                        i++;
                    }
                    catch (System.Exception ex)
                    {
                        pos = 0;
                        wbrAuto.Navigate("https://pay.zing.vn/home/logout.html");
                    }
                    break;
                case 2://Action
                    if (wbrAuto.Url.ToString().Contains("fail"))
                    {
                        pos = 1;
                        this.statusAutoText.Text = "LOGIN FAIL";
                        wbrAuto.Navigate("http://login.me.zing.vn");
                        dataGridView1.Rows.Add(i, acc[0], "Login Fail");
                        dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
                    }
                    else
                    {
                        pos = 9;
                        this.statusAutoText.Text = "LOGIN SUCCESSFUL";
                        wbrAuto.Navigate(txtLink.Text);
                        dataGridView1.Rows.Add(i, acc[0], "Login Successful");
                        dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
                    }
                    break;
                case 9:
                    //loadLike++;
                    //string strLike = "";
                    //string test;
                    if (this.wbrAuto.ReadyState == WebBrowserReadyState.Complete
                        && wbrAuto.Document.GetElementById("jvt_n") != null
                        && wbrAuto.Document.GetElementById("jvt_n").InnerText != "0")
                    {
                        timer.Stop();
                        dataGridView1.Rows.Add(i, acc[0], "Done. Total: " + wbrAuto.Document.GetElementById("jvt_n").InnerText);
                        dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
                        wbrAuto.Document.GetElementById("jvt_but1").InvokeMember("Click");
                        this.statusAutoText.Text = "LOGOUT";
                        wbrAuto.Navigate("https://pay.zing.vn/home/logout.html");
                        if (i == arrAcc.Count)
                        {
                            i = 0;
                            pos = 10;
                            MessageBox.Show("Successful");
                            Application.Exit();
                        }
                        else
                        {
                            pos = 0;
                        }
                    }
                    else
                    {
                        timer.Start();
                        dataGridView1.Rows.Add(i, acc[0], "Loading...");
                    }
                    
                    /*
                    if (loadLike == 4)
                    {
                        wbrAuto.Document.GetElementById("jvt_but1").InvokeMember("Click");
                        loadLike = 0;
                        if (i == arrAcc.Count)
                        {
                            pos = 10;
                        }
                        else
                        {
                            pos = 0;
                        }
                        dataGridView1.Rows.Add(i, acc[0], wbrAuto.Document.GetElementById("jvt_n").InnerText);
                        dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
                        this.statusAutoText.Text = "LOGOUT";
                        wbrAuto.Navigate("https://pay.zing.vn/home/logout.html");
                    }
                    */
                    break;
                case 10:
                    break;
                case 11:
                    MessageBox.Show("Stop");
                    break;
                default:
                    break;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnStop.Enabled = true;
            arrAcc = ReadFile("acc.txt"); 
            arrAcc = ShuffleAcc(arrAcc);
            pos = 0;
            this.statusAutoText.Text = "START";
            wbrAuto.Navigate("about:blank");
        }

        private void cbxLikeImage_CheckedChanged(object sender, EventArgs e)
        {
            if (cbxLikeImage.Checked)
            {
                txtLink.Enabled = false;
            }
            else
            {
                txtLink.Enabled = true;
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            pos = 12;
            wbrAuto.Document.GetElementById("u").SetAttribute("value", "tqninh05");
            wbrAuto.Document.GetElementById("pp").SetAttribute("value", "0916389552");
            wbrAuto.Document.GetElementById("bntlogin").InvokeMember("Click");
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            pos = 12;
            wbrAuto.Navigate(txtLink.Text);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            wbrAuto.Document.GetElementById("jvt_but1").InvokeMember("Click");
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            panel1.Visible = !panel1.Visible;
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            string test = wbrAuto.Document.GetElementById("jvt_n").InnerText;
            MessageBox.Show(test);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            pos = 0;
            this.statusAutoText.Text = "LOGOUT";
            wbrAuto.Navigate("https://pay.zing.vn/home/logout.html");
        }

        private ArrayList ShuffleAcc(ArrayList inputList)
        {
            ArrayList randomList = new ArrayList();

            Random r = new Random();
            int randomIndex = 0;
            while (inputList.Count > 0)
            {
                randomIndex = r.Next(0, inputList.Count); //Choose a random object in the list
                randomList.Add(inputList[randomIndex]); //add it to the new, random list
                inputList.RemoveAt(randomIndex); //remove to avoid duplicates
            }

            return randomList; //return the new random list
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == null)
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Image Files(*.png; *.jpg; *.bmp)|*.png; *.jpg; *.bmp";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    Bitmap result = new Bitmap(100, 100);
                    using (Graphics g = Graphics.FromImage((Image)result))
                        g.DrawImage(new Bitmap(open.FileName), 0, 0, 100, 100);
                    dataGridView1[e.ColumnIndex, e.RowIndex].Value = result;
                }
            }
        }
    }
}
